package com.wenwan.manage.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wenwan.manage.service.GoodsService;
import com.wenwan.manage.setting.DefaultValue;

import javax.annotation.Resource;

/**
 * 商品
 * 
 * @author Admin
 *
 */
@RequestMapping("/goods")
@Controller
public class GoodsController {

	@Autowired
	private GoodsService goodsService;

	/**
	 * 查询所有并分页
	 * 
	 * @param currentPage
	 *            第几页
	 * @param pageSize
	 *            每页显显条数
	 * @return
	 */
	@RequestMapping(value = "/select/all", method = { RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> selectAll(
			@RequestParam(required = false, defaultValue = DefaultValue.CURRENT_PAGE) Integer currentPage,
			@RequestParam(required = false, defaultValue = DefaultValue.PAGE_SIZE) Integer pageSize) {
		return goodsService.selectAll(currentPage, pageSize);
	}

	/**
	 * 查询单个
	 * 
	 * @param id
	 *            唯一标识
	 * @return
	 */
	@RequestMapping(value = "/select/one/by/id", method = { RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> selectOneById(@RequestParam(required = true) String id) {
		return goodsService.selectOneById(id);
	}

	/**
	 * 添加商品
	 * 
	 * @param userId
	 *            所属用户ID
	 * @param goodsCategoryId
	 *            商类分类ID
	 * @param title
	 *            商品标题
	 * @param price
	 *            商品价格
	 * @param info
	 *            商品详情
	 * @param imagesCarousel
	 *            轮播图集合
	 * @param imagesInfo
	 *            商品详情图片
	 * @return
	 */
	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> create(@RequestParam(required = true) String userId,
			@RequestParam(required = true) String goodsCategorySecondId, @RequestParam(required = true) String title,
			@RequestParam(required = true) Double price, @RequestParam(required = true) String info,
			@RequestParam(required = true) String imagesCarousel, @RequestParam(required = true) String imagesInfo) {
		return goodsService.create(userId, goodsCategorySecondId, title, price, info, imagesCarousel, imagesInfo);
	}

	/**
	 * 更新商品信息
	 * 
	 * @param id
	 *            唯一标识
	 * @param userId
	 *            所属用户ID
	 * @param goodsCategoryId
	 *            商类分类ID
	 * @param title
	 *            商品标题
	 * @param price
	 *            商品价格
	 * @param info
	 *            商品详情
	 * @param imagesCarousel
	 *            轮播图集合
	 * @param imagesInfo
	 *            商品详情图片
	 * @return
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> update(@RequestParam(required = true) String id,
			@RequestParam(required = false) String userId, @RequestParam(required = false) String goodsCategorySecondId,
			@RequestParam(required = false) String title, @RequestParam(required = false) Double price,
			@RequestParam(required = false) String info, @RequestParam(required = false) String imagesCarousel,
			@RequestParam(required = false) String imagesInfo, @RequestParam(required = false) Integer countReading,
			@RequestParam(required = false) Integer countShare, @RequestParam(required = false) Integer countWell) {
		return goodsService.update(id, userId, goodsCategorySecondId, title, price, info, imagesCarousel, imagesInfo,
				null, null, null);
	}

	/**
	 * 删除商品
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete/by/id", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> deleteById(@RequestParam(required = true) String id) {
		return goodsService.deleteById(id);
	}

}
